package com.otod.pm.prj.service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import com.otod.base.service.BaseService;
import com.otod.commons.utils.DateUtils;
import com.otod.commons.utils.StringUtils;
import com.otod.pm.constants.PmConstant;
import com.otod.pm.prj.vo.ProjectVo;
import com.otod.pm.prj.vo.TaskVo;
import com.otod.sys.amcm.service.AmcmMsgAppService;

/**
 * 定时任务
 * @author yiChen
 */
public class TaskSchedulService extends BaseService{
	
	 @Autowired
	 private ProjectService projectService;
	 
	 @Autowired
	 private AmcmMsgAppService msgService;
	 
	 @Autowired
	 private TaskService taskService;
	
	@Scheduled(cron="0 34,40,45 * * * ?")//设置定时时间每天凌晨1点触发该任务状态随时间变更
	public void autoSchedul(){
		Map<String,Object> map = new HashMap<String,Object>(); 
		
		List<ProjectVo> projectList  = projectService.findAll();
		for (ProjectVo projectVo : projectList) {
			if(PmConstant.PRJ_STATUS_STARTING.equals(projectVo.getStatus())){
				map.put("prjId", projectVo.getUuid());
				List<TaskVo> taskList = taskService.queryTaskList(map);
				for (TaskVo taskVo : taskList) {
					
					Integer days = DateUtils.getDaysBetweenTwoDates(DateUtils.formatDate(taskVo.getStartDate(),"yyyyMMdd"), DateUtils.getDate("yyyyMMdd"));
					if(days>=0&&StringUtils.isNotEmpty(taskVo.getEmpNo())
							&&PmConstant.TASK_STATUS_READY.equals(taskVo.getStatus())){
						
						map = new HashMap<String,Object>();
						map.put("uuid", taskVo.getUuid());
						map.put("status", PmConstant.TASK_STATUS_STARTING);
						taskService.update(map);
						msgService.installMsgVo(taskVo,taskVo.getTaskName()+"任务 ：开始时间到了，任务已经自动开始！");
					}
					Integer endDays = DateUtils.getDaysBetweenTwoDates(DateUtils.formatDate(taskVo.getPlanEndDate(),"yyyyMMdd"), DateUtils.getDate("yyyyMMdd"));
					if(days>-3&&StringUtils.isNotEmpty(taskVo.getEmpNo())
							&&PmConstant.TASK_STATUS_STARTING.equals(taskVo.getStatus())){
						msgService.installMsgVo(taskVo,taskVo.getTaskName()+"任务 ： 结束时间快到了，请注意提交任务！");
					}
				}
			}
		}
	}
	
	
}
